using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using Sedna.Core.Security;
using Sedna.Domain;

namespace Sedna.Core.Controller.Configurations
{
    public class WeightStringTranslator : IDataTranslator
    {
        private string format = "{0}";
        private IPrincipalWrapper principalWrapper;
        private bool addOZpostfix = false;
        private bool clearIfZero = false;

        public IPrincipalWrapper PrincipalWrapper
        {
            set { principalWrapper = value; }
        }

        public string Format
        {
            set { format = value; }
        }

        public bool AddOZpostfix
        {
            set { addOZpostfix = value; }
        }

        public bool ClearIfZero
        {
            set { clearIfZero = value; }
        }
        
        public object Translate(Control control, object value)
        {
            string strValue;

            if (value == null)
            {
                strValue = "0";
            }
            else // value isn't null
            {
                if (value is Weight)
                    strValue = string.Format(principalWrapper.Culture, format, (value as Weight).Value);
                else
                    strValue = string.Format(principalWrapper.Culture, format, value);
            }

            if (strValue.Equals("0") && clearIfZero)
                return string.Empty;

            return (addOZpostfix) ? strValue + " OZ" : strValue;            
        }

        public object Translate(Control control, object value, Type targetType)
        {
            return Translate(control, value);            
        }
    }
}
